﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Gollie.Menu
{
	public class MenuItem : MenuItemCollection
	{
		string text;
		string href;
		bool isSeparator;
		string target;
		string itemCss;
		string onClick;

		public MenuItem()
			: base()
		{
		}

		public MenuItem(XmlNode node)
		{
			this.Populate(node);
		}

		internal void Populate(XmlNode node)
		{
			foreach (XmlNode childNode in node.ChildNodes)
			{
				if (childNode.LocalName == "item")
				{
					MenuItem menuItem = new MenuItem(childNode);

					this.Add(menuItem);
				}

			}

			this.ReadAttributes(node);
		}

		private void ReadAttributes(XmlNode node)
		{
			foreach (XmlAttribute attr in node.Attributes)
			{
				switch (attr.LocalName)
				{
					case "text":
						this.isSeparator = (attr.Value == "-");

						if (!this.isSeparator)
							this.text = attr.Value;
						break;

					case "href": this.href = attr.Value;
						break;

					case "target": this.target = attr.Value;
						break;

					case "css": this.itemCss = attr.Value;
						break;

					case "onclick": this.onClick = attr.Value;
						break;
				}
			}
		}

		public string Text
		{
			get { return this.text; }
		}

		public string Href
		{
			get { return this.href; }
		}

		public string Target
		{
			get { return this.target; }
		}

		public bool IsSeparator
		{
			get { return this.isSeparator; }
		}

		public string ItemCss
		{
			get { return this.itemCss; }
		}

		public string OnClick
		{
			get { return this.onClick; }
		}
	}
}